--Avatar
--create by zouyb
--游戏人物外观

local direction = require "game/ui/base/Direction";

--Avatar继承自Node
Avatar = class("Avatar", function() 
		return cc.Node:create();
	end);
	
function Avatar.create(filename)
	local node = Avatar.new(filename); --new函数会调用ctor构造函数
	return node;
end

--构造函数
function Avatar:ctor(filename)
	--载入动画
    self.animNode = cc.CSLoader:createNode(filename);
    self.animAction = cc.CSLoader:createTimeline(filename);
    print("duration:"..self.animAction:getDuration()..",speed:"..self.animAction:getTimeSpeed());
    self.animNode:runAction(self.animAction);	
    self:addChild(self.animNode)    
	
	--初始化方向
	self.dir = direction.LEFT;
end

--播放
function Avatar:play(name)
    if name == "idle" then
        self.animAction:setTimeSpeed(0.4);    
        self.animAction:gotoFrameAndPlay(3, 15, true);
    elseif name == "run" then
        self.animAction:setTimeSpeed(0.8);    
        self.animAction:gotoFrameAndPlay(15, 33, true);
    end    
end

--停止
function Avatar:stop()
	self.animAction:pause();
end

--设置方向
function Avatar:setDir(dir)
    if dir == direction.LEFT then
        self.animNode:setScaleX(-1);	
		self.dir = direction.LEFT;
    else
        self.animNode:setScaleX(1);
		self.dir = direction.RIGHT;
    end	
end

--获取方向
function Avatar:getDir(dir)
	return self.dir;
end